﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YJKRGeometry;

namespace YJKOpeningCommon.OpeningEX
{
	public class OpeningXData
	{
		public static bool IsIncludeSchema(Element elem3D)
		{
			if (elem3D == null)
			{
				return false;
			}
			Guid guid = new Guid(OpeningXData.m_SparyGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				return false;
			}
			Entity entity = elem3D.GetEntity(schema);
			return entity.IsValid() && entity.Schema != null;
		}

		public void SetLineUniqueId(List<string> lstLineGuid, Element elem)
		{
			if (elem == null)
			{
				return;
			}
			Guid guid = new Guid(OpeningXData.m_SparyGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				this.ProvideSparyschemaData(ref schema, guid);
			}
			if (schema == null)
			{
				return;
			}
			Entity entity = new Entity(schema);
			this.ProvideEntityData(ref entity, schema);
			Field field = schema.GetField("str2DUniqueId");
			entity.Set<string>(field, this.m_str2DUniqueId);
			field = schema.GetField("strDimUniqueId");
			entity.Set<string>(field, this.m_strDimUniqueId);
			field = schema.GetField("lstLineGUID");
			entity.Set<IList<string>>(field, lstLineGuid);
			this.CommitEntity(ref elem, entity);
		}

		public void SetDimUniqueId(string uniqueId, Element elem3D)
		{
			if (elem3D == null)
			{
				return;
			}
			Guid guid = new Guid(OpeningXData.m_SparyGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				this.ProvideSparyschemaData(ref schema, guid);
			}
			if (schema == null)
			{
				return;
			}
			Entity entity = new Entity(schema);
			this.ProvideEntityData(ref entity, schema);
			Field field = schema.GetField("str2DUniqueId");
			entity.Set<string>(field, this.m_str2DUniqueId);
			field = schema.GetField("strDimUniqueId");
			entity.Set<string>(field, uniqueId);
			field = schema.GetField("lstLineGUID");
			entity.Set<IList<string>>(field, this.m_lstLineGuid);
			this.CommitEntity(ref elem3D, entity);
		}

		public void Set2DUniqueId(string uniqueId, Element elem3D)
		{
			if (elem3D == null)
			{
				return;
			}
			Guid guid = new Guid(OpeningXData.m_SparyGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				this.ProvideSparyschemaData(ref schema, guid);
			}
			if (schema == null)
			{
				return;
			}
			Entity entity = new Entity(schema);
			this.ProvideEntityData(ref entity, schema);
			Field field = schema.GetField("str2DUniqueId");
			entity.Set<string>(field, uniqueId);
			this.CommitEntity(ref elem3D, entity);
		}

		public void WriteOpeningXData(Element elem3D)
		{
			Guid guid = new Guid(OpeningXData.m_SparyGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				this.ProvideSparyschemaData(ref schema, guid);
			}
			if (schema == null)
			{
				return;
			}
			Entity entity = new Entity(schema);
			this.ProvideEntityData(ref entity, schema);
			this.CommitEntity(ref elem3D, entity);
		}

		private void ProvideSparyschemaData(ref Schema Sparyschema, Guid Sparyguid)
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(Sparyguid);
			schemaBuilder.SetSchemaName("HY_Opening_XData");
			schemaBuilder.AddSimpleField("strDesignUser", typeof(string));
			schemaBuilder.AddSimpleField("strCreateOpeningDate", typeof(string));
			schemaBuilder.AddSimpleField("strSpeciality", typeof(string));
			schemaBuilder.AddSimpleField("str2DUniqueId", typeof(string));
			schemaBuilder.AddSimpleField("strDimUniqueId", typeof(string));
			schemaBuilder.AddSimpleField("strHostUniqueId", typeof(string));
			schemaBuilder.AddSimpleField("openingSpecialy", typeof(int));
			FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField("openingHeight", typeof(double));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("openingWidth", typeof(double));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("openingPenetrationThickness", typeof(double));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("openingAngle", typeof(double));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("openingMaxZ", typeof(double));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("openingMinZ", typeof(double));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("hostWidth", typeof(double));
			fieldBuilder.SetUnitType(0);
			schemaBuilder.AddSimpleField("strAxisX", typeof(string));
			schemaBuilder.AddSimpleField("strAxisY", typeof(string));
			schemaBuilder.AddSimpleField("strHostObject", typeof(string));
			schemaBuilder.AddSimpleField("strOpeningSize", typeof(string));
			fieldBuilder = schemaBuilder.AddSimpleField("directionProfile", typeof(XYZ));
			fieldBuilder.SetUnitType(0);
			fieldBuilder = schemaBuilder.AddSimpleField("position", typeof(XYZ));
			fieldBuilder.SetUnitType(0);
			schemaBuilder.AddArrayField("lstLineGUID", typeof(string));
			Sparyschema = schemaBuilder.Finish();
		}

		private void ProvideEntityData(ref Entity entity, Schema Sparyschema)
		{
			Field field = Sparyschema.GetField("strDesignUser");
			entity.Set<string>(field, this.m_strDesignUser);
			field = Sparyschema.GetField("strCreateOpeningDate");
			entity.Set<string>(field, this.m_strCreateOpeningDate);
			field = Sparyschema.GetField("strSpeciality");
			entity.Set<string>(field, this.m_strSpeciality);
			field = Sparyschema.GetField("openingSpecialy");
			entity.Set<int>(field, this.m_eOpeningProfile);
			field = Sparyschema.GetField("openingWidth");
			entity.Set<double>(field, this.m_dWidth, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("openingHeight");
			entity.Set<double>(field, this.m_dHeight, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("openingPenetrationThickness");
			entity.Set<double>(field, this.m_dPenetrationThickness, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("openingAngle");
			entity.Set<double>(field, this.m_dAngle, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("openingMaxZ");
			entity.Set<double>(field, this.m_dMaxZ, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("openingMinZ");
			entity.Set<double>(field, this.m_dMinZ, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("hostWidth");
			entity.Set<double>(field, this.m_dHostWidth, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("strAxisX");
			entity.Set<string>(field, this.m_strAxisX);
			field = Sparyschema.GetField("strAxisY");
			entity.Set<string>(field, this.m_strAxisY);
			field = Sparyschema.GetField("strHostObject");
			entity.Set<string>(field, this.m_strHostObject);
			if (this.m_eOpeningProfile == 0)
			{
				this.m_strOpeningSize = AssistFunc.feetToMM(this.m_dWidth).ToString("f0");
			}
			else
			{
				this.m_strOpeningSize = AssistFunc.feetToMM(this.m_dWidth).ToString("f0") + "X" + AssistFunc.feetToMM(this.m_dHeight).ToString("f0");
				if (Geometry.LessThan(0.0, this.m_dPenetrationThickness))
				{
					this.m_strOpeningSize = this.m_strOpeningSize + "X" + AssistFunc.feetToMM(this.m_dPenetrationThickness).ToString("f0");
				}
			}
			field = Sparyschema.GetField("strOpeningSize");
			entity.Set<string>(field, this.m_strOpeningSize);
			field = Sparyschema.GetField("directionProfile");
			entity.Set<XYZ>(field, this.m_directionProfile, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("position");
			entity.Set<XYZ>(field, this.m_position, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = Sparyschema.GetField("strHostUniqueId");
			entity.Set<string>(field, this.m_strHostUniqueId);
		}

		private void CommitEntity(ref Element elem3D, Entity entity)
		{
			elem3D.SetEntity(entity);
		}

		public bool ReadOpeningXData(Element element)
		{
			try
			{
				Guid guid = new Guid(OpeningXData.m_SparyGuid);
				Schema schema = Schema.Lookup(guid);
				if (schema != null)
				{
					Entity entity = element.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						Field field = schema.GetField("strDesignUser");
						this.m_strDesignUser = entity.Get<string>(field);
						field = schema.GetField("strCreateOpeningDate");
						this.m_strCreateOpeningDate = entity.Get<string>(field);
						field = schema.GetField("strSpeciality");
						this.m_strSpeciality = entity.Get<string>(field);
						field = schema.GetField("str2DUniqueId");
						this.m_str2DUniqueId = entity.Get<string>(field);
						field = schema.GetField("openingSpecialy");
						this.m_eOpeningProfile = entity.Get<int>(field);
						field = schema.GetField("openingWidth");
						this.m_dWidth = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("openingHeight");
						this.m_dHeight = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("openingPenetrationThickness");
						this.m_dPenetrationThickness = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("openingAngle");
						this.m_dAngle = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("openingMaxZ");
						this.m_dMaxZ = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("openingMinZ");
						this.m_dMinZ = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("hostWidth");
						this.m_dHostWidth = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("strAxisX");
						this.m_strAxisX = entity.Get<string>(field);
						field = schema.GetField("strAxisY");
						this.m_strAxisY = entity.Get<string>(field);
						field = schema.GetField("strHostObject");
						this.m_strHostObject = entity.Get<string>(field);
						field = schema.GetField("strOpeningSize");
						this.m_strOpeningSize = entity.Get<string>(field);
						field = schema.GetField("directionProfile");
						this.m_directionProfile = entity.Get<XYZ>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("position");
						this.m_position = entity.Get<XYZ>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
						field = schema.GetField("strDimUniqueId");
						this.m_strDimUniqueId = entity.Get<string>(field);
						field = schema.GetField("strHostUniqueId");
						this.m_strHostUniqueId = entity.Get<string>(field);
						field = schema.GetField("lstLineGUID");
						this.m_lstLineGuid = entity.Get<IList<string>>(field).ToList<string>();
						return true;
					}
				}
			}
			catch (Exception)
			{
			}
			return false;
		}

		public bool IsContainOpeningObj(Element elem, ref bool isContain)
		{
			if (!this.ReadOpeningXData(elem))
			{
				return false;
			}
			if (this.m_strSpeciality == "未知")
			{
				isContain = false;
			}
			else
			{
				isContain = true;
			}
			return true;
		}

		public string m_strSpeciality;

		public string m_strCreateOpeningDate;

		public string m_strDesignUser;

		public string m_str2DUniqueId;

		public int m_eOpeningProfile;

		public double m_dHeight;

		public double m_dWidth;

		public double m_dPenetrationThickness;

		public double m_dMaxZ;

		public double m_dMinZ;

		public double m_dHostWidth;

		public string m_strAxisX;

		public string m_strAxisY;

		public string m_strHostObject;

		public string m_strOpeningSize;

		public XYZ m_directionProfile;

		public XYZ m_position;

		public string m_strDimUniqueId;

		public string m_strHostUniqueId;

		public double m_dAngle;

		public List<string> m_lstLineGuid;

		private static string m_SparyGuid = "B6D6E528-4059-470B-A3CD-1012C9FC3E03";
	}
}
